package Windows;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.json.JSONObject;

import Bean.Msg;
import MainThread.MyClient;
import Utils.SendMsgUtils;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.awt.event.ActionEvent;

public class login extends JFrame {

	private JPanel contentPane;
	private JTextField T_key;
	private JTextField T_user;
	private JButton B_login;
	private JTextField reg_keyConfrim;
	private JTextField reg_key;
	private JTextField reg_name;
	private JLabel t_error;
	private JButton btn_reg;
	private JTextField reg_id;

	private Socket s;

	/**
	 * Launch the application. 登录界面，实现连接数据库，登录用户以及注册用户 安卓端：
	 * 
	 */
	/*
	 * public static void main(String[] args) { EventQueue.invokeLater(new
	 * Runnable() { public void run() { try { login frame = new login();
	 * frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } }
	 * }); }
	 */

	/**
	 * Create the frame.
	 */
	public login(Socket s) {
		this.s = s;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 565, 400);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		JPanel p1 = new JPanel();

		JPanel p2 = new JPanel();

		t_error = new JLabel(" ");
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane
				.setHorizontalGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPane.createSequentialGroup()
								.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_contentPane.createSequentialGroup().addContainerGap()
												.addComponent(p1, GroupLayout.PREFERRED_SIZE, 238,
														GroupLayout.PREFERRED_SIZE)
												.addGap(18).addComponent(p2, GroupLayout.PREFERRED_SIZE, 272,
														GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_contentPane.createSequentialGroup().addGap(228).addComponent(
												t_error, GroupLayout.PREFERRED_SIZE, 65, GroupLayout.PREFERRED_SIZE)))
								.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
		gl_contentPane.setVerticalGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPane.createSequentialGroup()
						.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addGroup(gl_contentPane.createParallelGroup(Alignment.TRAILING, false)
								.addComponent(p1, 0, 0, Short.MAX_VALUE).addComponent(p2, GroupLayout.DEFAULT_SIZE, 323,
										Short.MAX_VALUE))
						.addGap(3).addComponent(t_error).addContainerGap()));

		JLabel lblSing = new JLabel("SING");

		JLabel lblNewLabel = new JLabel("用户名");

		JLabel lblNewLabel_1 = new JLabel("密码");

		JLabel lblNewLabel_2 = new JLabel("再次输入密码");

		reg_keyConfrim = new JTextField();
		reg_keyConfrim.setColumns(10);

		reg_key = new JTextField();
		reg_key.setColumns(10);

		reg_name = new JTextField();
		reg_name.setColumns(10);

		btn_reg = new JButton("注册");
		btn_reg.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String frist = reg_key.getText();
				String sec = reg_keyConfrim.getText();
				if (frist.equals(sec)) {
					Msg msg = new Msg();
					msg.setFrom("");
					msg.setLogin_reg_chat_ack(MyClient.REG);
					msg.setTo("server");
					msg.setContent(reg_name.getText() + ":" + frist);
					
					SendMsgUtils.sendMsg(s, new JSONObject(msg).toString());

				} else {
					t_error.setText("两次密码不一致");
				}
			}
		});

		reg_id = new JTextField();
		reg_id.setColumns(10);
		GroupLayout gl_p2 = new GroupLayout(p2);
		gl_p2.setHorizontalGroup(gl_p2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_p2.createSequentialGroup().addGroup(gl_p2.createParallelGroup(Alignment.LEADING)
						.addComponent(lblSing)
						.addGroup(gl_p2.createSequentialGroup().addGap(22)
								.addGroup(gl_p2.createParallelGroup(Alignment.LEADING).addComponent(lblNewLabel_2)
										.addComponent(lblNewLabel).addComponent(lblNewLabel_1))
								.addPreferredGap(ComponentPlacement.RELATED).addGroup(
										gl_p2.createParallelGroup(Alignment.LEADING, false).addComponent(reg_name)
												.addComponent(reg_key)
												.addComponent(reg_keyConfrim, GroupLayout.DEFAULT_SIZE, 149,
														Short.MAX_VALUE)
												.addComponent(reg_id))))
						.addContainerGap(25, Short.MAX_VALUE))
				.addGroup(gl_p2.createSequentialGroup().addContainerGap(118, Short.MAX_VALUE).addComponent(btn_reg)
						.addGap(97)));
		gl_p2.setVerticalGroup(gl_p2.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_p2.createSequentialGroup().addComponent(lblSing).addGap(70)
						.addComponent(reg_id, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
								GroupLayout.PREFERRED_SIZE)
						.addGap(18)
						.addGroup(gl_p2.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel)
								.addComponent(reg_name, GroupLayout.PREFERRED_SIZE, 18, GroupLayout.PREFERRED_SIZE))
						.addGap(18)
						.addGroup(
								gl_p2.createParallelGroup(Alignment.BASELINE)
										.addComponent(reg_key, GroupLayout.PREFERRED_SIZE, 21,
												GroupLayout.PREFERRED_SIZE)
										.addComponent(lblNewLabel_1))
						.addGap(18)
						.addGroup(
								gl_p2.createParallelGroup(Alignment.BASELINE).addComponent(lblNewLabel_2).addComponent(
										reg_keyConfrim, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
						.addGap(54).addComponent(btn_reg).addContainerGap(26, Short.MAX_VALUE)));
		p2.setLayout(gl_p2);

		JLabel lblLogin = new JLabel("LOGIN");

		T_user = new JTextField();
		T_user.setToolTipText("在此输入用户名");
		T_user.setColumns(10);

		T_key = new JTextField();
		T_key.setToolTipText("在此输入密码");
		T_key.setColumns(10);

		JLabel lblName = new JLabel("用户名");

		JLabel lblKey = new JLabel("密码");

		B_login = new JButton("登录");
		B_login.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				Msg msg = new Msg();
				msg.setLogin_reg_chat_ack(MyClient.LOGIN);
				msg.setContent(T_user.getText() + ":" + T_key.getText());
				msg.setFrom(T_user.getText());
				msg.setTo("server");
				SendMsgUtils.sendMsg(s, new JSONObject(msg).toString());
			}
		});

		GroupLayout gl_p1 = new GroupLayout(p1);
		gl_p1.setHorizontalGroup(gl_p1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_p1.createSequentialGroup()
						.addGroup(gl_p1.createParallelGroup(Alignment.LEADING).addComponent(lblLogin)
								.addGroup(gl_p1.createSequentialGroup().addGap(11).addComponent(lblKey).addGap(30)
										.addComponent(T_key, GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)))
						.addContainerGap())
				.addGroup(gl_p1.createSequentialGroup().addContainerGap(96, Short.MAX_VALUE).addComponent(B_login)
						.addGap(85))
				.addGroup(gl_p1.createSequentialGroup().addContainerGap().addComponent(lblName)
						.addPreferredGap(ComponentPlacement.RELATED, 19, Short.MAX_VALUE)
						.addComponent(T_user, GroupLayout.PREFERRED_SIZE, 163, GroupLayout.PREFERRED_SIZE)
						.addContainerGap()));
		gl_p1.setVerticalGroup(gl_p1.createParallelGroup(Alignment.LEADING).addGroup(gl_p1.createSequentialGroup()
				.addComponent(lblLogin).addGap(142)
				.addGroup(gl_p1.createParallelGroup(Alignment.BASELINE).addComponent(lblName).addComponent(T_user,
						GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
				.addGap(26)
				.addGroup(gl_p1.createParallelGroup(Alignment.BASELINE).addComponent(lblKey).addComponent(T_key,
						GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(ComponentPlacement.RELATED, 33, Short.MAX_VALUE).addComponent(B_login).addGap(59)));
		p1.setLayout(gl_p1);
		contentPane.setLayout(gl_contentPane);
	}

	public JTextField getReg_id() {
		return reg_id;
	}

	public void setReg_id(JTextField reg_id) {
		this.reg_id = reg_id;
	}

	public JButton getButton() {
		return B_login;
	}

	public JTextField getT_key() {
		return T_key;
	}

	public void setT_key(JTextField t_key) {
		T_key = t_key;
	}

	public JTextField getT_user() {
		return T_user;
	}

	public void setT_user(JTextField t_user) {
		T_user = t_user;
	}

	public JLabel getT_error() {
		return t_error;
	}

	public void setT_error(JLabel t_error) {
		this.t_error = t_error;
	}

	public JButton getBtn_reg() {
		return btn_reg;
	}

	public void setBtn_reg(JButton btn_reg) {
		this.btn_reg = btn_reg;
	}

	public JTextField getReg_keyConfrim() {
		return reg_keyConfrim;
	}

	public JTextField getReg_key() {
		return reg_key;
	}

	public JTextField getReg_name() {
		return reg_name;
	}
}
